<template>
  <div class="demand-details">
    <!-- 审核未通过框 -->
    <div class="notpassed" v-if="info.status === 4">
      <div class="title">
        <img src="~/assets/images/demand-notpassed.png" alt="" />
        审核失败
      </div>
      <div class="_content">
        {{ info.reason }}
      </div>
      <m-button class="edit" width="92" @click="handlerEdit">去修改</m-button>
    </div>

    <!-- 需求详情 -->
    <div class="title">需求详情</div>
    <div class="form-wrap">
      <div class="inquiry-date f14">
        <div class="date">{{ info.create_time }}</div>
        发起询盘
      </div>
      <m-form
        ref="form"
        :model="formData"
        label-width="105px"
        label-position="left"
      >
        <m-form-item label="产品名称：" style="margin-bottom: 0">
          <p class="name f16">{{ info.goods_info }}</p>
        </m-form-item>
        <m-form-item label="产品类型：" style="margin-bottom: 0">
          <p class="type f16">
            {{ info.goods_type == 1 ? "包装材料" : "配套服务" }}
          </p>
        </m-form-item>
        <m-form-item label="产品类型：" style="margin-bottom: 0">
          <p class="classifi f16">
            {{ info.class1.cate_name }} <span v-if="info.class2">></span> 
            {{ info.class2 && info.class2.cate_name }}
          </p>
        </m-form-item>
        <m-form-item label="产品数量：" style="margin-bottom: 0">
          <p class="quantity f16"> {{ info.goods_num }} {{ info.goods_unit_txt }}</p>
        </m-form-item>
        <m-form-item label="贸易条件：" style="margin-bottom: 0">
          <p class="condition f16">{{ info.goods_condition }}</p>
        </m-form-item>
        <m-form-item label="期望单价：" style="margin-bottom: 0">
          <p class="price f16">
            {{ info.goods_price }} {{ info.currency == 0 ? "人民币" : "元" }}
          </p>
        </m-form-item>
        <div v-show="showRemaining">
          <m-form-item
          label="产品图片："
          style="margin-bottom: 0; font-size: 16px"
        >
          <div class="pics-box" v-if="info.img_list && info.img_list[0] !== 'undefined'">
            <div
              class="pic-item"
              v-for="(item, index) in info.img_list"
              :key="index"
            >
              <img :src="item" alt="" class="objectCover" />
            </div>
          </div>
          <div class="content f16" v-else>--</div>
        </m-form-item>
        <m-form-item label="产品附件：" style="margin-bottom: 0;">
          <div class="annex-list" v-if="info.file_list && info.file_list[0] !== 'undefined'">
            <div
              class="annex"
              v-for="(item, index) in info.file_list"
              :key="index"
            >
              <img src="@/assets/images/pdf-icon.png" alt="" class="pic" />
              <p class="name">{{ info.file_list }}</p>
            </div>
          </div>
          <div class="content f16" v-else>--</div>
        </m-form-item>
          <m-form-item label="运输方式：" style="margin-bottom: 0">
            <p class="transform f16">{{ info.transport_type }}</p>
          </m-form-item>
          <m-form-item label="目的地：" style="margin-bottom: 0">
            <p class="destination f16">{{ info.province }} {{ info.city }}</p>
          </m-form-item>
          <m-form-item label="详细地址：" style="margin-bottom: 0">
            <p class="address f16">{{ info.address || '--' }}</p>
          </m-form-item>
          <m-form-item label="期望交期：" style="margin-bottom: 0">
            <p class="expect f16">{{ info.days || '--' }}天</p>
          </m-form-item>
          <m-form-item label="支付方式：" style="margin-bottom: 0">
            <p class="payment f16">
              {{ info.pay_type == 0 ? "线上支付" : "线下支付" }}
            </p>
          </m-form-item>
          <m-form-item label="备注：" style="margin-bottom: 0">
            <p class="remarks f16">{{ info.remarks }}</p>
          </m-form-item>
        </div>
      </m-form>
      <div class="fold" @click="showRemaining = !showRemaining">
        {{ showRemaining ? "收起详情" : "展开详情" }}
        <img v-if="!showRemaining" src="~/assets/images/fold-icon.png" alt="" />
        <img
          v-else
          src="~/assets/images/fold-icon.png"
          alt=""
          style="transform: rotateX(180deg)"
        />
      </div>
      <div class="btns" v-if="info.status !== 9">
        <m-button
          width="123"
          type="grey"
          @click="() => showDialog = true"
          style="margin-right: 15px"
          >取消
        </m-button>
        <m-button
          @click="handlerEdit"
          width="123"
          type="grey"
          style="margin-right: 15px"
          >去编辑
        </m-button>
        <m-button v-if="info.status == 2" @click="showTc" width="123">立即发布</m-button>
      </div>
    </div>

    <!-- 商家信息 -->
    <div class="title">商家信息</div>
    <div class="company-list">
      <div class="item" v-for="(item, index) in info.merchant" :key="index">
        <img :src="item.mer_avatar" alt="" class="pic" />
        <div class="item-wrap">
          <p class="name">{{ item.mer_name }}</p>
          <div class="operation">
            <m-button
              v-if="item.is_content == 1"
              width="80"
              size="mini"
              style="margin-right: 10px"
              @click="toDetail(item.mer_id)"
              >已响应
            </m-button>
            <m-button v-else width="80" size="mini" type="dark" @click="toDetail(item.mer_id)">未响应</m-button>
          </div>
        </div>
      </div>
    </div>
    <m-dialog title="平台提示" :show-dialog.sync="showDialogPublic">
      <p class="text" style="margin-bottom: 50px; font-size: 16px; line-height: 26px">
        发布则默认您已阅读，理解并同意找包装网的委托平台规则。 是否立即发布？
      </p>
      <div class="btns" style="display: flex; justify-content: center">
        <m-button
          width="180"
          type="dark"
          @click="closeTc"
          style="margin-right: 10px"
          >取消</m-button>
        <m-button width="180" @click="publish">确定</m-button>
      </div>
    </m-dialog>
    <!-- 取消询价 -->
    <cancel-demand :showDialog.sync='showDialog' :demandId='id' @handleClose='handleClose' />
  </div>
</template>

<script>
import MButton from "@/components/common/m-button/MButton";
import MForm from "@/components/common/m-form/MForm";
import MFormItem from "@/components/common/m-form/MFormItem";
import MInput from "@/components/common/m-input/MInput";
import MUpload from "@/components/common/m-upload/MUpload";
import MDialog from "@/components/common/m-dialog/MDialog";
import MSelect from "@/components/common/m-select/MSelect";
import MRadio from "@/components/common/m-radio/MRadio";
import { RadioGroup } from "element-ui";
import { requirementDe, apiContentDem,pubMyDemands } from "@/request/api";
import CancelDemand from '../components/cancelDemand.vue'

export default {
  name: "DemandMerInfo",
  components: {
    MUpload,
    MInput,
    MFormItem,
    MForm,
    MButton,
    MDialog,
    MSelect,
    MRadio,
    RadioGroup,
    CancelDemand
  },
  data() {
    return {
      id: "",
      info: {
        class1: "",
        class2: "",
        status: 0,
        merchant:[]
      },
      formData: {},
      showRemaining: false,
      replyFormData: {},
      showDialog: false,
      recommendInfo: [],
      fileListData: [],
      showDialogPublic: false,
    };
  },
  created() {
    this.id = this.$route.params.id;
    this.getInfo();
  },
  methods: {
    /** 获取需求详情 */
    getInfo() {
      requirementDe(this.id).then(res => {
        if (res.status === 200) {
          this.info = res.data;
          this.getRecommendInfo();
        } else {
          this.$toast.show(res.message, 1000, false);
        }
      }).catch(err => {
        this.$message.error(err.message)
      })
    },
    /** 关闭 */
    handleClose() {
      this.showDialog = false;
      this.$router.push('/profile/purchase-management/my-demand')
    },
    /** 商家推荐商品 */
    getRecommendInfo() {
      const id = this.id;
      apiContentDem(id).then(res => {
        if (res.status === 200) {
          this.info.merchant = res.data;
        }else {
          this.$message.error(res.message)
        }
      }).catch(err => {
        this.$message.error(err.message);
      });
    },
    /** 已响应的进入详情 */
    toDetail(id) {
      if(this.info.status === 9) return false;
      this.$router.push({ path: '/profile/purchase-management/my-demand/merInfo/' + id, query: { uid: this.info.id} })
    },
    /** 去编辑 */
    handlerEdit() {
      this.$router.push({ path: "/require", query: { id: this.info.id } });
    },
    // 关闭平台提示发布委托
    closeTc() {
      this.showDialogPublic = false
      this.optionId = ""
    },
    // 显示平台提示发布委托
    showTc(id) {
      this.showDialogPublic = true
      this.optionId = this.info.id
    },
    // 发布委托
    publish() {
      let data = {
        id: this.optionId,
        status: 1
      }
      pubMyDemands(data).then((res) => {
        if (res.status === 200) {
          this.$toast.show("发布成功", 1000, false)
           this.showDialogPublic = false
          this.getInfo()
        } else {
          this.$toast.show(res.message, 1000, false)
          this.showDialogPublic = false
        }
      }).catch((err) => {
        this.$message.error(err.message)
      })
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/assets/style/common.scss";

/deep/ .noBorder .el-button {
  border: none !important;
}

.demand-details {
  margin-top: 30px;
  font-size: 16px;
  color: #666;

  .pics-box {
    display: flex;

    .pic-item {
      width: 160px;
      height: 160px;
      border: 1px solid #eee;
      border-radius: 10px;
      margin-right: 15px;
      overflow: hidden;
      margin-bottom: 20px;
    }
  }

  .number {
    margin-bottom: 20px;
  }

  .notpassed {
    border: solid 1px #eaeaea;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 15px 30px;
    position: relative;
    margin-bottom: 50px;

    .title {
      color: $theme-color-1;
      font-size: 20px;
      font-weight: bold;
      display: flex;
      align-items: center;
      margin-bottom: 25px;

      img {
        margin-right: 10px;
      }
    }

    ._content {
      line-height: 22px;
      margin-bottom: 100px;
    }

    .edit {
      position: absolute;
      right: 15px;
      bottom: 15px;
    }
  }

  > .title {
    width: 145px;
    height: 48px;
    line-height: 48px;
    text-align: center;
    color: #fff;
    background-color: $theme-color-1;
  }

  .form-wrap {
    border: solid 1px #eaeaea;
    padding: 15px 20px;
    position: relative;
    margin-bottom: 50px;

    .fold {
      margin-top: 20px;
      color: $theme-color-1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      cursor: pointer;

      img {
        margin-top: 5px;
      }
    }

    .btns {
      display: flex;
      position: absolute;
      right: 0;
      top: -55px;
    }

    .annex-list {
      display: flex;
      align-items: center;

      .annex {
        width: 100px;
        // text-align: left;
        display: flex;
        flex-direction: column;
        align-items: center;

        .pic {
          width: 34px;
          height: 34px;
        }

        .name {
          font-size: 14px;
          @include text-omitted();
        }
      }
    }

    .inquiry-date {
      position: absolute;
      right: 15px;
      top: 15px;
      color: #969696;
      display: flex;
      flex-direction: column;
      align-items: center;
      background-color: rgb(243, 244, 247);
      padding: 15px;
      border: solid 1px #eaeaea;

      .date {
        margin-bottom: 5px;
      }
    }
  }

  .company-list {
    padding: 15px 20px;
    border: solid 1px #eaeaea;

    .item {
      width: calc((100% - 40px) / 2);
      padding: 15px;
      display: inline-flex;
      align-items: center;
      background-color: rgb(243, 244, 247);
      border: solid 1px #eaeaea;
      margin: 10px;
      .item-wrap {
        height: 80px;
        display: flex;
        flex-direction: column;
        justify-content: center;
      }
      .pic {
        width: 130px;
        height: 80px;
        margin-right: 15px;
      }

      .operation {
        display: flex;
        align-items: center;
        margin-top: 20px;
      }
    }
  }

  .chat-wrap {
    margin-top: 50px;
    padding: 15px 20px;
    border: solid 1px #eaeaea;

    .company-title {
      display: flex;
      align-items: center;
      margin-bottom: 40px;

      .pic {
        width: 148px;
        height: 76px;
        margin-right: 15px;
      }
    }

    .recommend-title {
      padding-left: 20px;
      border-left: solid 2px #666;
      margin-bottom: 15px;
    }

    .list {
      margin: -10px;

      .item {
        padding: 15px;
        border: solid 1px #eaeaea;
        display: inline-flex;
        flex-direction: column;
        align-items: center;
        width: calc((100% - 80px) / 4);
        margin: 10px;

        .pic {
          width: 170px;
          height: 170px;
          object-fit: cover;
          //background-color: #ccc;
          margin-bottom: 15px;
        }

        .name {
          @include text-omitted(1);
          margin-bottom: 15px;
        }

        .price-and-collect {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #969696;
          margin-bottom: 15px;

          .keywords {
            color: $theme-color-1;
            font-weight: bold;
            margin-right: 5px;
          }
        }

        .storage-and-cart {
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          font-size: 14px;
          color: #969696;

          .keywords {
            color: $theme-color-1;
            font-weight: bold;
            margin-right: 5px;
          }
        }
      }
    }

    .chat {
      margin-top: 50px;

      .title-merchant {
        min-width: 266px;
        padding: 0 15px;
        display: inline-block;
        height: 48px;
        line-height: 48px;
        text-align: center;
        color: #fff;
        background-color: rgb(0, 133, 232);
      }

      .title-user {
        min-width: 266px;
        padding: 0 15px;
        display: inline-block;
        height: 48px;
        line-height: 48px;
        text-align: center;
        color: #fff;
        background-color: $theme-color-1;
      }

      .chat-merchant,
      .chat-user {
        &:not(:last-of-type) {
          margin-bottom: 30px;
        }
      }

      .textarea-merchant {
        min-height: 140px;
        padding: 15px 10px;
        border: solid 1px #eaeaea;
        color: #969696;
        line-height: 28px;
        background-color: rgb(243, 244, 247);
      }

      .textarea-user {
        min-height: 140px;
        padding: 15px 10px;
        border: solid 1px #eaeaea;
        color: #969696;
        line-height: 28px;
      }

      .reply {
        margin-top: 120px;
        padding-top: 50px;
        border-top: solid 1px #eaeaea;

        .title {
          color: $theme-color-1;
          margin-bottom: 30px;
          font-weight: bold;
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
}
</style>
